/*
 * 文 件    名:  TrafficStats.java
 * 创建日期:  2013-3-15下午2:46:47
 * 版          权:  Epro Technologies Co., Ltd. Copyright 2011-2013,  All rights reserved
 * 作          者:  xrj66010
 */
package com.weipu.common.util;

import java.text.DecimalFormat;

import android.content.Context;
import android.net.TrafficStats;
import android.os.Process;

/** 
 * 用于监测流量与网速的工具类
 * @version [版本号, 2013-3-15]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 * 
 * @author xrj66010
 */
public class MyTrafficStat
{
	private static final String TAG="MyTrafficStat";
    //发送
    static float tx;
    
    //接收
    static float rx;
    
    //发送
    static float txsByte;
    
    //接收
    static float rxsByte;
    
    static float byteForTx;
    
    static float byteForRx;
    
    static int uid;
    
    public static float getByteForTx()
    {
        return byteForTx;
    }
    
    public static void setByteForTx(float byteForTx)
    {
        MyTrafficStat.byteForTx = byteForTx;
    }
    
    public static float getByteForRx()
    {
        return byteForRx;
    }
    
    public static void setByteForRx(float byteForRx)
    {
        MyTrafficStat.byteForRx = byteForRx;
    }
    
    public static float getTxsByte()
    {
        return txsByte;
    }
    
    public static void setTxsByte(float txsByte)
    {
        MyTrafficStat.txsByte = txsByte;
    }
    
    public static float getRxsByte()
    {
        return rxsByte;
    }
    
    public static void setRxsByte(float rxsByte)
    {
        MyTrafficStat.rxsByte = rxsByte;
    }
    
    public static int getUid()
    {
        return uid;
    }
    
    public static void setUid(int uid)
    {
        MyTrafficStat.uid = uid;
    }
    
    public static float getTx()
    {
        return tx;
    }
    
    public static void setTx(float tx)
    {
        MyTrafficStat.tx = tx;
    }
    
    public static float getRx()
    {
        return rx;
    }
    
    public static void setRx(float rx)
    {
        MyTrafficStat.rx = rx;
    }
    
    /**
      * 获取当前应用程序在进程中所分配的ID
      * @param context  [参数说明]
      * @return void [返回类型说明]
      * @exception throws [违例类型] [违例说明]
      * @see [类、类#方法、类#成员]
      */
    public static void getUID(Context context)
    {
        setUid(Process.myUid());
        //        ActivityManager manager = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
        //        List<RunningAppProcessInfo> list = manager.getRunningAppProcesses();
        //        for (int i = 0; i < list.size(); i++)
        //        {
        //            if ("com.weipu.dx".equals(list.get(i).processName))
        //            {
        //                int uids = list.get(i).uid;
        //                setUid(uids);
        //                break;
        //            }
        //        }
    }
    
    /**
      * 获取发送与接收数据大小（单位：KB）
      * @param interfaceName  [参数说明]
      * @return void [返回类型说明]
      * @exception throws [违例类型] [违例说明]
      * @see [类、类#方法、类#成员]
      */
    public static void getRxbytes(String interfaceName)
    {
        if (getUid() != 0)
        {
            if (0.0 != getByteForRx() && 0.0 != getByteForTx())
            {
                DecimalFormat df = new DecimalFormat("0.000");
                Logger.d(TAG, "调用名为：" + interfaceName + "--发送数据--" + df.format(getByteForTx()) + " KB");
                Logger.d(TAG, "调用名为：" + interfaceName + "--接收数据--" + df.format(getByteForRx()) + " KB");
            }
            else
            {
                float txsByte = (TrafficStats.getUidTxBytes(getUid()));
                float rxsByte = (TrafficStats.getUidRxBytes(getUid()));
                Logger.d(TAG,
                    "调用名为：" + interfaceName + "--发送数据--" + String.format("%.3f", (txsByte - getTxsByte()) / 1024)
                        + " KB");
                Logger.d(TAG,
                    "调用名为：" + interfaceName + "--接收数据--" + String.format("%.3f", (rxsByte - getRxsByte()) / 1024)
                        + " KB");
                setTxsByte(txsByte);
                setRxsByte(rxsByte);
            }
        }
    }
    
    /**
      *测试一段时间里的网速
      * @param interfaceName
      * @param oldTime
      * @param nowTime  [参数说明]
      * @return void [返回类型说明]
      * @exception throws [违例类型] [违例说明]
      * @see [类、类#方法、类#成员]
      */
    public static void getNetSpeed(String interfaceName, Long oldTime, Long nowTime)
    {
        if (getUid() != 0)
        {
            float time = nowTime - oldTime;
            float txs = (TrafficStats.getUidTxBytes(getUid()));
            float rxs = (TrafficStats.getUidRxBytes(getUid()));
            DecimalFormat df = new DecimalFormat("0.000");
            String strTx = df.format((txs - getTx()) / 1024 / (time / 1000));
            String strRx = df.format((rxs - getRx()) / 1024 / (time / 1000));
            if (0.0 != getTx() && 0.0 != getRx())
            {
                Logger.d(TAG, "调用名为：" + interfaceName + "--发送速率为--" + strTx + " KB/s");
                Logger.d(TAG, "调用名为：" + interfaceName + "--接收速率为--" + strRx + " KB/s");
                setByteForTx((txs - getTx()) / 1024);
                setByteForRx((rxs - getRx()) / 1024);
                setTx(txs);
                setRx(rxs);
            }
            else
            {
                setTx(txs);
                setRx(rxs);
                Logger.d(TAG, "此次数据无效");
            }
        }
    }
}
